package com.ant.storage.controller;

import com.ant.storage.common.ResultPage;
import com.ant.storage.pojo.AllocationWarehouseEnter;
import com.ant.storage.pojo.AllocationWarehouseOut;
import com.ant.storage.service.IAllocationWarehouseEnterService;
import com.ant.storage.service.IAllocationWarehouseOutService;
import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/allocationWarehouseOut")
@Api(tags = "调拨出库接口")
public class AllocationWarehouseOutController {

    @Autowired
    IAllocationWarehouseOutService allocationWarehouseOutService;

    @Autowired
    IAllocationWarehouseEnterService allocationWarehouseEnterService;

    @ApiOperation(value = "调拨出库单查询")
    @PostMapping("/queryAllocationWarehouseOut")
    public ResultPage queryAllocationWarehouseOut(@RequestBody PageRecv<AllocationWarehouseOut> pageRecv){
        return allocationWarehouseOutService.queryAllocationWarehouseOut(pageRecv);
    }

    @ApiOperation(value = "调拨出库单编辑")
    @PostMapping("/allocationWarehouseOutEdit")
    public ResultVo allocationWarehouseOutEdit(@RequestBody AllocationWarehouseOut allocationWarehouseOut){
        return allocationWarehouseOutService.allocationWarehouseOutEdit(allocationWarehouseOut,2);
    }

    @ApiOperation(value = "调拨出库单确认出库")
    @PostMapping("/allocationWarehouseOutConfirm")
    @Transactional(rollbackFor = Exception.class)
    public ResultVo allocationWarehouseOutConfirm(@RequestBody AllocationWarehouseOut allocationWarehouseOut){
        allocationWarehouseOutService.allocationWarehouseOutEdit(allocationWarehouseOut,3);
        //扣减对应库存
        allocationWarehouseOutService.stockSubtract(allocationWarehouseOut);
        //调拨入库单生成
        AllocationWarehouseEnter allocationWarehouseEnter = new AllocationWarehouseEnter();
        allocationWarehouseEnter.setAllocation_warehouse_out_id(allocationWarehouseOut.getAllocation_warehouse_out_id());
        allocationWarehouseEnter.setAllocation_warehouse_enter_type(1);
        allocationWarehouseEnterService.save(allocationWarehouseEnter);
        return ResultVo.success("ok");
    }

}
